package org.bozo.im.service.protocols.xmpp.stanza.processor.starttls;

import java.io.IOException;

import org.bozo.im.service.Constants;
import org.bozo.im.service.protocols.xmpp.XMPPSession;
import org.bozo.im.service.protocols.xmpp.stanza.Stanza;
import org.bozo.im.service.protocols.xmpp.stanza.processor.StanzaProcessor;
import org.bozo.im.service.protocols.xmpp.stanza.task.starttls.StartTlsTask;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.util.Log;

public class RequiredStanzaProcessor extends StanzaProcessor {

	private static RequiredStanzaProcessor mInstance;

	private RequiredStanzaProcessor() {}

	public static RequiredStanzaProcessor getInstance() {
		if (mInstance == null)
			mInstance = new RequiredStanzaProcessor();
		return mInstance;
	}

	@Override
	public Stanza getStanza() {
		return Stanza.STARTTLS_REQUIRED;
	}

	@Override
	public void processStanza(XmlPullParser pParser, XMPPSession pSession, StanzaProcessor pParentProcessor) throws XmlPullParserException,	IOException {

		if (pParentProcessor == StartTlsStanzaProcessor.getInstance()) {
			// ---------------------------------------------------------------------------------------
			Log.d(Constants.ACTIVITY_TAG, "org.bozo.im.xmpp.stanza.processor.starttls.StartTlsStanzaProcessor.processStanza ---------- It's time to start TLS negotiation ! The server asks for it...");
			// ---------------------------------------------------------------------------------------
			pSession.getTaskProcessor().pushTask(new StartTlsTask("0"));

		} else {
			// ---------------------------------------------------------------------------------------
			Log.d(Constants.ACTIVITY_TAG, "org.bozo.im.xmpp.stanza.processor.starttls.RequiredStanzaProcessor.processStanza ---------- I don't know what to do with this Stanza");
			// ---------------------------------------------------------------------------------------
		}
	}

}
